package structuralModel.combinationModel.genericOrganization;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 功能描述:通用组织客户
 * 2024/08/22
 *
 * @author ZhangYi
 */
public class GenericOrganizationClient {
	public static void main(String[] args) {
		String resultStr = getResultStr();
		List<PhysicalBoundary> physicalBoundaries = JSONArray.parseArray(resultStr, PhysicalBoundary.class);

		Set<String> reqOrgCodeSet = new HashSet<>();
		reqOrgCodeSet.add("100605");
		reqOrgCodeSet.add("100160");

		List<PhysicalBoundary> boundaries = physicalBoundaries
				.stream()
				.sorted(Comparator.comparing(PhysicalBoundary::getParentCode))
				.collect(Collectors.toList());

		Map<String, List<PhysicalBoundary>> parentCodePhysicalBoundarysMap
				= physicalBoundaries.stream().collect(Collectors.groupingBy(PhysicalBoundary::getParentCode));

		List<APhysicalBoundaryCompoent> physicalBoundaryCompoents = new ArrayList<>();
		Map<String,APhysicalBoundaryCompoent> orgCodeCompoentMap = new HashMap<>();
		// 避免重复处理 Set
		Set<String> visitedOrgCodeSet = new HashSet<>();
		for (PhysicalBoundary boundary : boundaries) {
			if (visitedOrgCodeSet.contains(boundary.getOrgCode())){
				continue;
			}

			// 一级根节点信息
			if (reqOrgCodeSet.contains(boundary.getOrgCode())){
				GenericPhysicalBoundaryRootCompoent rootCompoent = new GenericPhysicalBoundaryRootCompoent(boundary);
				physicalBoundaryCompoents.add(rootCompoent);
				orgCodeCompoentMap.put(boundary.getOrgCode(), rootCompoent);
				visitedOrgCodeSet.add(boundary.getOrgCode());
			}else {
				APhysicalBoundaryCompoent root = orgCodeCompoentMap.get(boundary.getParentCode());
				if (Objects.isNull(root)){
					continue;
				}
				List<PhysicalBoundary> subBoundaryList
						= parentCodePhysicalBoundarysMap.get(boundary.getParentCode());
				if (CollectionUtils.isEmpty(subBoundaryList)){
					// 没有子级,是叶子节点
					GenericPhysicalBoundaryLeafCompoent leafCompoent = new GenericPhysicalBoundaryLeafCompoent(boundary);
					root.addElement(leafCompoent);
					orgCodeCompoentMap.put(boundary.getOrgCode(), leafCompoent);
					visitedOrgCodeSet.add(boundary.getOrgCode());
					continue;
				}

				for (PhysicalBoundary physicalBoundary : subBoundaryList) {
					GenericPhysicalBoundaryLevelCompoent<PhysicalBoundary> levelCompoent = new GenericPhysicalBoundaryLevelCompoent<>(boundary);
					root.addElement(levelCompoent);
					visitedOrgCodeSet.add(physicalBoundary.getOrgCode());
					orgCodeCompoentMap.put(physicalBoundary.getOrgCode(), levelCompoent);
				}
			}
		}


		// 目前泛型,打印的值是泛型,[{}] 转换有问题,但是不影响使用,physicalBoundaryCompoents 是有值的
		System.out.println(JSON.toJSONString(physicalBoundaryCompoents));

	}



	/**
	 * 获取结果str
	 *
	 * @return {@code String }
	 * @author ZhangYi
	 */
	private static String getResultStr(){
		return "[\n" +
				"  {\n" +
				"    \"id\": 209,\n" +
				"    \"org_code\": \"100000\",\n" +
				"    \"org_name\": \"中国外运股份有限公司\",\n" +
				"    \"org_level\": \"2\",\n" +
				"    \"parent_code\": \"000000\",\n" +
				"    \"org_path\": \"/000000/100000\",\n" +
				"    \"org_path_name\": \"中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.559998\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 210,\n" +
				"    \"org_code\": \"100605\",\n" +
				"    \"org_name\": \"中外运空运有限公司\",\n" +
				"    \"org_level\": \"3\",\n" +
				"    \"parent_code\": \"100000\",\n" +
				"    \"org_path\": \"/000000/100000/100605\",\n" +
				"    \"org_path_name\": \"中外运空运有限公司/中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.560194\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 211,\n" +
				"    \"org_code\": \"100160\",\n" +
				"    \"org_name\": \"中外运物流有限公司\",\n" +
				"    \"org_level\": \"3\",\n" +
				"    \"parent_code\": \"100000\",\n" +
				"    \"org_path\": \"/000000/100000/100160\",\n" +
				"    \"org_path_name\": \"中外运物流有限公司/中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.560369\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 212,\n" +
				"    \"org_code\": \"101441\",\n" +
				"    \"org_name\": \"SE Logistics Holding B.V.\",\n" +
				"    \"org_level\": \"3\",\n" +
				"    \"parent_code\": \"100000\",\n" +
				"    \"org_path\": \"/000000/100000/101441\",\n" +
				"    \"org_path_name\": \"SE Logistics Holding B.V./中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.560595\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 213,\n" +
				"    \"org_code\": \"100749\",\n" +
				"    \"org_name\": \"中外运空运日本株式会社\",\n" +
				"    \"org_level\": \"4\",\n" +
				"    \"parent_code\": \"100605\",\n" +
				"    \"org_path\": \"/000000/100000/100605/100749\",\n" +
				"    \"org_path_name\": \"中外运空运日本株式会社/中外运空运有限公司/中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.560784\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 214,\n" +
				"    \"org_code\": \"101522\",\n" +
				"    \"org_name\": \"中外运物流有限公司西北区域\",\n" +
				"    \"org_level\": \"4\",\n" +
				"    \"parent_code\": \"100160\",\n" +
				"    \"org_path\": \"/000000/100000/100160/101522\",\n" +
				"    \"org_path_name\": \"中外运物流有限公司西北区域/中外运物流有限公司/中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.560920\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 215,\n" +
				"    \"org_code\": \"101349\",\n" +
				"    \"org_name\": \"中外运空运有限公司长沙分公司\",\n" +
				"    \"org_level\": \"4\",\n" +
				"    \"parent_code\": \"100605\",\n" +
				"    \"org_path\": \"/000000/100000/100605/101349\",\n" +
				"    \"org_path_name\": \"中外运空运有限公司长沙分公司/中外运空运有限公司/中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.561063\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 216,\n" +
				"    \"org_code\": \"101328\",\n" +
				"    \"org_name\": \"中外运空运有限公司北京分公司\",\n" +
				"    \"org_level\": \"4\",\n" +
				"    \"parent_code\": \"100605\",\n" +
				"    \"org_path\": \"/000000/100000/100605/101328\",\n" +
				"    \"org_path_name\": \"中外运空运有限公司北京分公司/中外运空运有限公司/中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.561236\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 217,\n" +
				"    \"org_code\": \"101192\",\n" +
				"    \"org_name\": \"中外运物流西北有限公司\",\n" +
				"    \"org_level\": \"5\",\n" +
				"    \"parent_code\": \"101522\",\n" +
				"    \"org_path\": \"/000000/100000/100160/101522/101192\",\n" +
				"    \"org_path_name\": \"中外运物流西北有限公司/中外运物流有限公司西北区域/中外运物流有限公司/中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.561382\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 218,\n" +
				"    \"org_code\": \"100618\",\n" +
				"    \"org_name\": \"北京中外运嘉航物流有限公司\",\n" +
				"    \"org_level\": \"5\",\n" +
				"    \"parent_code\": \"101328\",\n" +
				"    \"org_path\": \"/000000/100000/100605/101328/100618\",\n" +
				"    \"org_path_name\": \"北京中外运嘉航物流有限公司/中外运空运有限公司北京分公司/中外运空运有限公司/中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.561518\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  }\n" +
				"]";
	}
}
